package com.farewell.hotel.controller;

import com.farewell.hotel.entity.Result;
import com.farewell.hotel.pojo.Room;
import com.farewell.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Farewell
 * @date 2021/4/23
 */
@Controller
public class RoomController {
    @Autowired
    private RoomService roomService;

    @RequestMapping("getRoomsByStatus.do")
    @ResponseBody
    public List<Room> getRoomsByStatus() {
        return roomService.getRoomsByStatusWithUsed();
    }


    /**
     * 查找空闲房间
     * @param httpSession 缓存
     * @return checkin.html
     */
    @RequestMapping("spareRoomList.do")
    public String addInRoomInfoRequest(HttpSession httpSession) {
        List<Room> roomList = roomService.findSpareRoom();
        httpSession.setAttribute("roomList", roomList);
        return "bill/checkin.jsp";
    }

    @RequestMapping("getRoomInfo.do")
    @ResponseBody
    public List<Room> getRoomInfo() {
        return roomService.getRoomInfo();
    }

    @RequestMapping("changeRoomFromCleanToSpareById.do")
    @ResponseBody
    public Boolean changeRoomFromCleanToSpareById(@RequestParam("id")Long id) {
        try {
            roomService.changeRoomFromCleanToSpareById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @RequestMapping("addRoom.do")
    public String addRoom(Room room) {
        try {
            roomService.addRoom(room);
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败, 请重试";
        }
        return "room/showRoom.html";
    }

}
